package org.example;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.lang.Math;

public class Main {
    public static int getSecondValue(int[] arr) {
        int max = arr[0] > arr[1] ? arr[0] : arr[1];
        int sec_max = (max == arr[0]) ? (sec_max = arr[1]) : (sec_max = arr[0]);
        for (int i = 2; i < arr.length; i++)
        {
            if (arr[i] > max)
            {
                sec_max = max;
                max = arr[i];
            }
        }
        return sec_max;
    }

    public static void getCode() {
        String code = "";
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            if (random.nextInt(2) == 0)
            {
                code += (char)('A' + random.nextInt(26));
            }
            else
                code += (Integer)(0 + random.nextInt(10));
        }
        System.out.println(code);
    }

    private static int binarySearch(int[] arr, int i) {
        Arrays.sort(arr);
        int left = 0, right = arr.length - 1;
        while (left < right)
        {
            int mid = (left + right) / 2;
            if (arr[mid] > i)
                right = mid - 1;
            else if (arr[mid] < i)
                left = mid + 1;
            else
                return mid;
        }
        return -1;
    }

    private static void bubblesort(int[] arr) {
        if (arr == null || arr.length == 0)
            return;
        boolean swapped;
        for (int i = 0; i < arr.length - 1; i++)
        {
            swapped = false;
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    swapped = true;
                }
            }
            System.out.println(Arrays.toString(arr));
            if (!swapped)
                break;
        }
    }

    private static void selectionSort(int[] arr) {
        int minIndex = 0;
        for (int i = 0; i < arr.length - 1; i++)
        {
            minIndex = i;
            for (int j = i + 1; j < arr.length; j++)
            {
                if (arr[minIndex] > arr[j])
                    minIndex = j;
            }

            if (minIndex != i)
            {
                int tmp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i] = tmp;
            }
            System.out.println(Arrays.toString(arr));
        }
    }

    private static void insertionSort(int[] arr) {
        int len = arr.length;
        if (len <= 1)
            return;
        for (int i = 1; i < len; i++)
        {
            int value = arr[i], j = i - 1;
            for (; j >= 0; j--)
            {
                if (value < arr[j])
                    arr[j + 1] = arr[j];
                else
                    break;
            }
            arr[j + 1] = value;
            System.out.println(Arrays.toString(arr));
        }

    }

    public static void main(String[] args) {
        int[] arr = {56, 34, 45, 12, 67, 52, 4};
        insertionSort(arr);
        /*
        int[] arr = {56, 34, 45, 12, 67, 52, 4};
        selectionSort(arr);

        int[] arr = {56, 34, 45, 12, 67, 52, 4};
        bubblesort(arr);


        Random random = new Random();
        int[] arr1 = new int[100];
        int[] arr2 = new int[7];
        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = random.nextInt(6) + 1;
            arr2[arr1[i]]++;
        }
        System.out.println(Arrays.toString(arr2));




        Random random = new Random();
        int[] arr = new int[20];
        for (int i = 0; i < arr.length; i++)
            arr[i] = random.nextInt(50) + 1;
        System.out.println(Arrays.toString(arr));
        Scanner sc = new Scanner(System.in);
        while (true)
        {
            int data = sc.nextInt();
            if (data == -1)
                break;
            int index = binarySearch(arr, data);
            if (index != -1)
                System.out.println(index);
            else
                System.out.println("用户数据不存在!");
        }



        getCode();




        Random random = new Random();
        for (int i = 0; i < 10; i++)
            System.out.print((random.nextInt(100) + 1) + " ");



        int[] arr1 = {1, 2, 3};
        int[] arr2 = arr1;
        arr2[1] = 0;
        System.out.println("arr1: " + Arrays.toString(arr1));
        System.out.println("arr2: " + Arrays.toString(arr2));


        int[] arr1 = {1, 2, 3};
        for (int num : arr1)
            num = 4;
        System.out.println("输出: " + arr1[0]);


        int arr[] = new int[5];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++)
            arr[i] = sc.nextInt();
        System.out.println(getSecondValue(arr));

        int arr[][] = new int[3][3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                arr[i][j] = sc.nextInt();

        for (int i = 0; i < arr[i].length; i++)
            System.out.println(Arrays.toString(arr[i]));


        String[] arr = new String[]{"hello", "world", "!"};

        System.out.println(Arrays.toString(arr));

        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + " X");

        char[] vector1 = new char[]{'a', 'b', 'c'};
        System.out.println(Arrays.toString(vector1));

        char[] vector2 = new char[3];
        vector2[0] = 'a';
        vector2[1] = 'b';
        vector2[2] = 'c';
        System.out.println(Arrays.toString(vector2));

        char[] vector3 = new char[3];
        for (int i = 0; i < vector3.length; i++)
        {
            vector3[i] = (char)('a' + i);
        }
        System.out.println(Arrays.toString(vector3));
        */
    }




}